package com.tuanzi.loan.business.repository.statistics.impl;

import com.tuanzi.loan.business.entity.statistics.StatHitSuccess;
import com.tuanzi.loan.business.repository.statistics.StatHitSuccessRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import org.jooq.Record;
import org.jooq.impl.TableImpl;
import org.springframework.stereotype.Repository;

import java.sql.Date;

import static com.tuanzi.loan.generated.jooq.stat.tables.StatHitSuccess.STAT_HIT_SUCCESS;

/**
 * @author Simon
 * @date 2019/08/02 16:03
 */
@Repository
public class StatHitSuccessRepositoryImpl extends JOOQBaseRepository implements StatHitSuccessRepository {

    protected TableImpl getTable() {
        return STAT_HIT_SUCCESS;
    }

    public StatHitSuccess findOneBy(String platform,String prdId, String channel, String hitLirType, String isNewUser, java.util.Date date) {
        Record record = dslContext.select().from(STAT_HIT_SUCCESS).where(STAT_HIT_SUCCESS.PLATFORM.eq(platform))
                .and(STAT_HIT_SUCCESS.PRD_ID.eq(Short.valueOf(prdId)))
                .and(STAT_HIT_SUCCESS.CHANNEL.eq(Integer.valueOf(channel))).and(STAT_HIT_SUCCESS.HIT_LIB_TYPE.eq(Short.valueOf(hitLirType)))
                .and(STAT_HIT_SUCCESS.CDATE.eq(new Date(date.getTime()))).and(STAT_HIT_SUCCESS.NEW_USER.eq(Byte.valueOf(isNewUser))).fetchOne();
        return record == null ? null : record.into(StatHitSuccess.class);
    }

    public void saveOrUpdate(String platform,String prdId, String channel,String isNewUser, java.util.Date date,String hitType, Integer hitSuccessCount) {
        StatHitSuccess record = findOneBy(platform, prdId, channel, hitType,isNewUser, date);

        if (record == null) {
            record=new StatHitSuccess();
            copyPojo(platform,prdId, channel, isNewUser, date, hitType, hitSuccessCount, record);
            insert(platform,prdId,channel,isNewUser,date,hitType,hitSuccessCount);
            return;
        }
        copyPojo(platform,prdId, channel, isNewUser, date, hitType, hitSuccessCount, record);
        update(record,platform,prdId,channel,isNewUser,date,hitType,hitSuccessCount);
    }
    public void insert(String platform,String prdId, String channel,String isNewUser, java.util.Date date,String hitType, Integer hitSuccessCount){
        dslContext.insertInto(STAT_HIT_SUCCESS,STAT_HIT_SUCCESS.PLATFORM,STAT_HIT_SUCCESS.PRD_ID,STAT_HIT_SUCCESS.CHANNEL,STAT_HIT_SUCCESS.NEW_USER,STAT_HIT_SUCCESS.CDATE,STAT_HIT_SUCCESS.HIT_LIB_TYPE,STAT_HIT_SUCCESS.HIT_SUCCESS)
                .values(platform,Short.valueOf(prdId),Integer.valueOf(channel),Byte.valueOf(isNewUser),new Date(date.getTime()),Short.valueOf(hitType),Integer.valueOf(hitSuccessCount)).execute();
    }
    public void update(StatHitSuccess record,String platform,String prdId, String channel,String isNewUser, java.util.Date date,String hitType, Integer hitSuccessCount){
        dslContext.update(STAT_HIT_SUCCESS)
                .set(STAT_HIT_SUCCESS.PLATFORM,platform)
                .set(STAT_HIT_SUCCESS.PRD_ID,Short.valueOf(prdId))
                .set(STAT_HIT_SUCCESS.CHANNEL,Integer.valueOf(channel))
                .set(STAT_HIT_SUCCESS.NEW_USER,Byte.valueOf(isNewUser))
                .set(STAT_HIT_SUCCESS.CDATE,new Date(date.getTime()))
                .set(STAT_HIT_SUCCESS.HIT_LIB_TYPE,Short.valueOf(hitType))
                .set(STAT_HIT_SUCCESS.HIT_SUCCESS,Integer.valueOf(hitSuccessCount))
                .where(STAT_HIT_SUCCESS.ID.eq(record.getId())).execute();
    }

    private void copyPojo(String platform,String prdId, String channel, String isNewUser, java.util.Date date, String hitType, Integer hitSuccessCount, StatHitSuccess record) {
        record.setPlatform(platform);
        record.setPrdId(Short.valueOf(prdId));
        record.setChannel(Integer.valueOf(channel));
        record.setHitLibType(Short.valueOf(hitType));
        record.setNewUser(Byte.valueOf(isNewUser));
        record.setHitSuccess(hitSuccessCount);
        record.setCdate(new Date(date.getTime()));
    }
}
